package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.RegionQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.utils.JSONResult;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/regions")
public class RegionController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ServletContext ctx;

    @GetMapping
    @ResponseBody
    public Object list(){
        //获取所有的地区
        List<Region> list = regionService.list();
        return list;
    }

    @GetMapping("/{state}")
    @ResponseBody
    public Object list(@ModelAttribute("qo") RegionQueryObject qo){
        //获取状态为推荐状态的所有的地区
        qo.setPageSize(0);
        return  regionService.query(qo);
    }

    //获取第二页之后的分页数据
    @GetMapping(value = "/{regionId}/strategies",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Object get(@ModelAttribute("qo") StrategyQueryObject qo) {
        //查询出当前地区所有的攻略
        return strategyService.query(qo);
    }


    //获取当季攻略和全部攻略
    @GetMapping(value = "/{regionId}/strategies",produces = "text/html;charset=utf-8")
    @ResponseBody
    public void get(@ModelAttribute("qo") StrategyQueryObject qo, HttpServletResponse resp) throws Exception {

        //查询出当前地区所有的攻略
        PageInfo alls = strategyService.query(qo);

        //查询出当前地区的攻略状态为推荐的攻略(不需要分页)
        qo.setPageSize(0);
        qo.setState(2L);
        PageInfo commends = strategyService.query(qo);

        //利用freemarker:数据+模板--->输出

        //创建配置对象
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        //获取模板的文件位置
        cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("templates")));
        //设置文件模板
        Template temp = cfg.getTemplate("strategy.ftl","utf-8");
        //合并模板和数据模型
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("allStrategies",alls);
        map.put("commendStrategies",commends);
        temp.process(map, resp.getWriter());

    }

    //查询所有攻略的标题---------------------------------------
    @GetMapping("/search")
    @ResponseBody
    public Object getStrategies(){
        return strategyService.searchTitle();
    }

    //根据标题搜索攻略跳转--------------------------------------
    @GetMapping("/titles")
    @ResponseBody
    public Object getStrategies(String title){
        return strategyService.selectStrategyByTitle(title);
    }



}
